package gao.xiaolei.dao;

import java.util.List;

import gao.xiaolei.entity.GroupMember;
import gao.xiaolei.entity.GroupMemberId;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository("GroupMemberDao")
public interface GroupMemberDao extends JpaRepository<GroupMember, GroupMemberId>{

	@Query(value = "select id.userId from GroupMember where id.groupId= :groupId")
	public List<String> queryGroupMember(@Param("groupId") String groupId);

	@Query(value = "select id.groupId from GroupMember where id.userId= :userId")
	public List<String> queryGroupByMember(@Param("userId") String userId);
	
	@Query(value = "select id.userId from GroupMember where id.groupId= :groupId and isLeader=1")
	public String queryGroupLeader(@Param("groupId") String groupId);
	
	@Query(value = "select id.groupId from GroupMember where id.userId= :userId and isLeader=1")
	public List<String> queryGroupByLeader(@Param("userId") String userId);

	@Query(value = "select count(*) from GroupMember where id.userId= :userId and isLeader=1")
	public Integer getLeadGroupCount(@Param("userId") String userId);

	public List<GroupMember> findAllByIdIn(List<GroupMemberId> ids);

	@Query(value = "from GroupMember where id.groupId= :groupId")
	public List<GroupMember> findAllByGroupId(@Param("groupId") String groupId);

	@Modifying
	@Query(value = "delete from GroupMember where id.groupId= :groupId")
	public void deleteAllByGroupId(@Param("groupId") String groupId);

}
